/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    /// <summary>
    /// 编辑器的配置
    /// </summary>
    public class ActionEditorConfig : ScriptableObject
    {
        public int standFrameUnitWidth = 10; //  标准帧单位宽度  单位每一帧10个像素
        public int maxFrameWidthLV = 10;
        public int frameUnitWidth = 10; //  当前帧单位宽度 
        public int defaultFrameRote = 30; //  默认帧率


        public static ActionEditorConfig GetOrCreateSettings()
        {
            // 查找或创建 CustomSettings 对象
            var path = ActionEditorUtility.GetAssetPath("t:guiskin", "ActionEditorConfig.guiskin");
            path = path.Replace("ActionEditorConfig.guiskin", "");

            var data = AssetDatabase.LoadAssetAtPath<ActionEditorConfig>(path + "ActionEditorConfig.asset");

            if (data == null)
            {
                ActionEditorConfig config = ScriptableObject.CreateInstance<ActionEditorConfig>();
                AssetDatabase.CreateAsset(data, path + nameof(ActionEditorConfig) + ".asset");
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            return data;
        }
    }


    public class ActionEditorConfigPreferencesProvider : SettingsProvider
    {
        private ActionEditorConfig settings;

        public ActionEditorConfigPreferencesProvider(string path, SettingsScope scope = SettingsScope.User)
            : base(path, scope)
        {
        }

        [SettingsProvider]
        public static SettingsProvider CreateCustomPreferencesProvider()
        {
            // 创建 PreferencesProvider 对象，并指定路径和范围
            var provider =
                new ActionEditorConfigPreferencesProvider("Preferences/ActionEditorConfig", SettingsScope.User);

            // 打开 Preferences 面板时默认显示的选项
            provider.keywords = new[] { "ActionEditor Config" };
            return provider;
        }

        public override void OnActivate(string searchContext, VisualElement rootElement)
        {
            // 加载自定义设置
            settings = ActionEditorConfig.GetOrCreateSettings();
        }

        public override void OnGUI(string searchContext)
        {
            base.OnGUI(searchContext);

            var frameUnitWidth = EditorGUILayout.IntField("当前帧单位宽度", settings.frameUnitWidth);
            if (frameUnitWidth != settings.frameUnitWidth)
            {
                if (frameUnitWidth < 5)
                {
                    frameUnitWidth = 5;
                }

                settings.frameUnitWidth = frameUnitWidth;
                EditorUtility.SetDirty(settings);
                AssetDatabase.SaveAssets();
            }

            var defaultFrameRote = EditorGUILayout.IntField("默认帧率", settings.defaultFrameRote);
            if (defaultFrameRote != settings.defaultFrameRote)
            {
                if (defaultFrameRote < 5)
                {
                    defaultFrameRote = 5;
                }

                settings.defaultFrameRote = defaultFrameRote;
                EditorUtility.SetDirty(settings);
                AssetDatabase.SaveAssets();
            }
        }
    }
}
